package com.app.configuration;

/*
  @author 阿伟
  @create 2020-11-23:15:52
  Swagger配置类
*/

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.VendorExtension;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    //配置Docket信息
    @Bean
    public Docket docket(Environment environment){
        /*
          设置swagger的显示环境
        * spring.profiles.active=dev  开发阶段
        */
        Profiles profiles = Profiles.of("dev","test");
        boolean acceptsProfiles = environment.acceptsProfiles(profiles);
        return new Docket(DocumentationType.SWAGGER_2)  //swagger版本号
                .apiInfo(apiInfo())                    //info信息
                .groupName("app")                     //分组名
                .enable(acceptsProfiles)             //为true表示开启swagger
                .select()                           //配置包扫描
                .apis(RequestHandlerSelectors.basePackage("com.app.controller"))
                .paths(PathSelectors.ant("/**"))
                .build();
    }

    //配置info
    public ApiInfo apiInfo(){
       return new ApiInfo(
               //标题
               "Swagger",
               //描述
               "app接口",
               //版本
               "1.0",
               //链接地址
               "urn:tos",
               //联系人信息
               new Contact("阿伟", "www.cctv.com", "806648464@qq.com"),
               //Apache版本
               "Apache 2.0",
               //Apache许可链接
               "http://www.apache.org/licenses/LICENSE-2.0",
               new ArrayList<VendorExtension>());
    }
}
